package cn.elricyu.tt.bean;

import java.text.MessageFormat;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

public class ActionBean {
	private static String PARAM_SEP = "\\|";
	private static String PARAM_PRO_SIGN = "^";
	private String url;
	private String params;
	private String desc;

	public ActionBean(String url, String params, String desc) {
		super();
		this.url = url;
		this.params = params;
		this.desc = desc;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getUrl(User self) {
		if (StringUtils.isBlank(params)) {
			return url;
		}
		String[] paramArray = params.split(PARAM_SEP);
		Object[] arguments = new Object[paramArray.length];
		for (int i = 0; i < paramArray.length; i++) {
			String param = paramArray[i];
			String value = paramArray[i];
			if (param.startsWith(PARAM_PRO_SIGN)) {
				String pro = param.substring(PARAM_PRO_SIGN.length());
				try {
					value = BeanUtils.getSimpleProperty(self, pro);
				} catch (Exception e) {
				}
			}
			arguments[i] = value;
		}
		String destUrl = MessageFormat.format(url, arguments);
		return destUrl;
	}

}
